package com.lancoo.klgcourseware.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.bean.SpellCharacterBean;
import com.lancoo.klgcourseware.utils.KlgToolUtils;

import java.util.List;


/**
 * Created by pengmi on 2019/1/2.
 * Description:
 */

public class SlowCharacterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<SpellCharacterBean> spellCharacterBeans;
    private int showIndex=-1,trainMode=1;
    private boolean isQuality=true;

    public SlowCharacterAdapter(Context mContext, List<SpellCharacterBean> spellCharacterBeans, int trainMode) {
        this.mContext = mContext;
        this.spellCharacterBeans = spellCharacterBeans;
        this.trainMode=trainMode;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.klg_list_item_slow_character,null);
        SlowCharacterVh slowCharacterVh=new SlowCharacterVh(inflate);
        return slowCharacterVh;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        SlowCharacterVh slowCharacterVh= (SlowCharacterVh) viewHolder;
        slowCharacterVh.initData(spellCharacterBeans.get(position),position);
    }

    @Override
    public int getItemCount() {
        return spellCharacterBeans.size();
    }


    private class SlowCharacterVh extends RecyclerView.ViewHolder{
        public TextView tv_character;
        public View underLine;

        public SlowCharacterVh(@NonNull View itemView) {
            super(itemView);
            tv_character=itemView.findViewById(R.id.tv_character);
            underLine=itemView.findViewById(R.id.underline);
            underLine=itemView.findViewById(R.id.underline);
        }

        public void initData(SpellCharacterBean spellCharacterBean,int position){
            tv_character.setText(spellCharacterBean.getCharacter());
            if(trainMode==1){
                if(showIndex>=position){
                    tv_character.setTextColor(Color.parseColor("#004ce8"));
                    underLine.setBackgroundColor(Color.parseColor("#004ce8"));
                }else{
                    tv_character.setTextColor(Color.parseColor("#aae3f5"));
                    underLine.setBackgroundColor(Color.parseColor("#aae3f5"));
                }
                tv_character.setTextSize(KlgToolUtils.isPad(mContext)?36:30);
            }else{
                if(trainMode==0){
                    tv_character.setTextColor(Color.parseColor("#b9b9b9"));
                    underLine.setBackgroundColor(Color.parseColor("#b9b9b9"));
                }else{
                    tv_character.setTextColor(Color.parseColor(isQuality?"#3ccaf8":"#ff0000"));
                    underLine.setBackgroundColor(Color.parseColor(isQuality?"#ffffff":"#ff0000"));
                }
                tv_character.setTextSize(KlgToolUtils.isPad(mContext)?24:20);
            }
            underLine.setVisibility("-".equalsIgnoreCase(spellCharacterBean.getCharacter())||
                    "·".equalsIgnoreCase(spellCharacterBean.getCharacter())?View.INVISIBLE:View.VISIBLE);
        }
    }

    public void changeRepeat(int showIndex){
        this.showIndex=showIndex;
        notifyDataSetChanged();
    }

    public void setTrainMode(int trainMode,boolean isQuality){
        this.trainMode=trainMode;
        this.isQuality=true;
        showIndex=-1;
        notifyDataSetChanged();
    }

    public int getShowIndex(){
        return showIndex;
    }
}
